// pagesEnterprise/pages/companyDetail/companyDetail.js
import ajax from "../../../utils/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailId: null,
    tagActive: 1,
    formData: null,
    type: null,
    showShare: false
  },
  /**
   * 显示/隐藏分享框
   */
  shareEvent(event) {
    this.setData({ showShare: !(event.detail == 1) })
  },
  tagActiveEvent(event) {
    const index = event.currentTarget.dataset.index
    this.setData({ tagActive: index })
  },
  /**
   * 企业详情
   */
  async getDetailRequest() {
    const { detailId } = this.data
    const res = await ajax.get(`/enterprise/info?id=${detailId}`)
    this.setData({ formData: res.data })
  },
  async deleteEvent() {
    wx.showModal({
      title: '提示',
      content: '确定要删除该企业吗?',
      success: async (res) => {
        if (!res.confirm) return
        await ajax.get(`/enterprise/remove?id=${this.data.detailId}`)
        await ajax.showSuccess('操作成功')
        wx.navigateBack()
      }
    })
  },
  /**
   * 收藏事件
   */
  async followEvent () {
    const {detailId, formData} = this.data
    await ajax.post('/follow/add', {
      enterpriseId: detailId,
      state: formData.follow ? 0 : 1
    })
    ajax.showToast(formData.follow ? '已取消关注' : '已关注')
    this.getDetailRequest()
  },
  /**
   * 点赞事件
   */
  async supportEvent () {
    const {detailId, formData} = this.data
    await ajax.post('/wx/support/add', {
      enterpriseId: detailId,
      state: formData.spotSupport ? 0 : 1
    })
    ajax.showToast(formData.spotSupport ? '已取消点赞' : '已点赞')
    this.getDetailRequest()
  },
  callPhoneEvent (event) {
    ajax.makePhoneCall(event.currentTarget.dataset.phone)
  },
  editEvent() {
    wx.navigateTo({
      url: `/pagesEnterprise/pages/companyInsert/companyInsert?detailId=${this.data.detailId}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.shareId = options.shareId || null
    if (options.scene) {
      const entity = ajax.windowSearch(options.scene)
      options.type = 1
      options.detailId = entity.detailId
      app.globalData.shareId = entity.shareId || null
    }
    this.setData({type: options.type || null})
    this.data.detailId = options.detailId
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.detailId) this.getDetailRequest()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const { formData } = this.data
    const userInfo = wx.getStorageSync('userInfo')
    return {
      title: `${userInfo.nickName}邀请您预览${formData.name}`,
      imageUrl: formData.indexImage,
      path: `/pagesEnterprise/pages/companyDetail/companyDetail?detailId=${formData.id}&shareId=${userInfo.id}&type=1`
    }
  }
})